//
//  MainCollectionView.swift
//  WangYiNews
//
//  Created by lanou3g on 16/2/28.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

import UIKit

import Async

protocol TableConposedCollectionViewDelegate:class {
    func tableConposedCollectionView(collectionView:TableConposedCollectionView , didChangeWithIndex index:Int)
}

protocol CreateWithIndexable:class {
    init(index:Int,selectClosure:IndexpathResultClosure?)
}

typealias IndexpathResultClosure = (indexPath:NSIndexPath)->()

class TableConposedCollectionView: UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource{
    
    weak var changeDelegate : TableConposedCollectionViewDelegate?
    
    let tableSelectClosure : IndexpathResultClosure?
    
    var numberOfItems : Int{
        return 3
    }
    
    init(delegate:TableConposedCollectionViewDelegate,tableViewSelectClosure:IndexpathResultClosure){
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        changeDelegate = delegate
        tableSelectClosure = tableViewSelectClosure
        super.init(frame:CGRect.zero, collectionViewLayout:layout)
        self.delegate = self
        self.dataSource = self
        self.pagingEnabled = true
        self.registerClass(TableConposedCollectionViewCell.self, forCellWithReuseIdentifier:"cell")
        self.contentOffset = CGPoint.zero
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateWithIndex(index:Int){
        let x = CGFloat(index) * self.bounds.size.width
        self.setContentOffset(CGPoint(x:x, y:0), animated: true)
    }
    
//    collectinView的代理方法
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath:indexPath) as! TableConposedCollectionViewCell
        cell.setTableViewWithIndex(indexPath.row)
        cell.backgroundColor = UIColor.blueColor()
        return cell
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = scrollView.contentOffset.x / self.bounds.size.width
        self.changeDelegate?.tableConposedCollectionView(self, didChangeWithIndex:Int(index))
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.numberOfItems
    }
    
    var itemSizeHaveSet : Bool = false
    override func layoutSubviews() {
        super.layoutSubviews()
        if itemSizeHaveSet { return }
        if self.bounds.size.width < UIScreen.mainScreen().bounds.size.width { return }
        if let layout = self.collectionViewLayout as? UICollectionViewFlowLayout{
            var once : dispatch_once_t = 0
            dispatch_once(&once){
                layout.itemSize = self.bounds.size
                self.itemSizeHaveSet = true
           dispatch_async(dispatch_get_main_queue()){
                    self.reloadItemsAtIndexPaths([NSIndexPath(index:0)])
                }
            }
        }
    }
}
